//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.kqb.controller;


import cn.hutool.core.bean.BeanUtil;
import com.kqb.common.controller.base.BaseController;
import com.kqb.common.domain.AjaxResult;
import com.kqb.common.domain.page.PageResult;
import com.kqb.pojo.dto.SupplierDto;
import com.kqb.query.SupplierQuery;
import com.kqb.service.SupplierService;
import com.kqb.service.SupplierUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/api"})
@Api(
        tags = {"供应商管理"}
)
public class SupplierController extends BaseController {
    @Autowired
    SupplierService supplierService;

    @Autowired
    SupplierUserService supplierUserService;



    public SupplierController() {
    }

    /**
     * 保存供应商
     *
     * @param supplierDto
     * @return
     */
    @ApiOperation("保存供应商")
    @PostMapping({"/supplier"})
    public AjaxResult save(@RequestBody SupplierDto supplierDto) {
        int save = this.supplierService.save(supplierDto);
        return AjaxResult.success("保存成功", save);
    }

    /**
     * 查询供应商
     *
     * @param id
     * @return
     */
    @ApiOperation("查询供应商")
    @GetMapping({"/supplier"})
    public AjaxResult get(String id) {
        SupplierDto select = supplierService.select(id);
        return AjaxResult.success("供应商信息", select);
    }

    /**
     * 修改供应商
     *
     * @param supplierDto
     * @return
     */
    @ApiOperation("修改供应商")
    @PutMapping({"/supplier"})
    public AjaxResult update(@RequestBody SupplierDto supplierDto) {
        int update = this.supplierService.update(supplierDto);
        return AjaxResult.success("修改成功", update);
    }

    /**
     * 删除供应商
     *
     * @param id
     * @return
     */
    @ApiOperation("删除供应商")
    @DeleteMapping({"/supplier"})
//    @ApiImplicitParam(
//            name = "id",
//            value = "供应商id",
//            dataTypeClass = Integer.class,
//            required = true)
    public AjaxResult delete(@RequestParam(required = true) int id) {
        int delete = this.supplierService.delete(id);
        return AjaxResult.success("删除成功", delete);
    }

    /**
     * 查询供应商列表
     *
     * @param query
     * @return
     */
    @ApiOperation("查询供应商列表")
    @PostMapping({"/supplier/list"})
    public AjaxResult query(@RequestBody SupplierQuery query) {
        PageResult qu = this.supplierService.queryByMap(BeanUtil.beanToMap(query));
        System.out.println(qu);
        return AjaxResult.success("查询成功", qu);
    }


    /**
     * 查询账号是否存在
     * @param account
     * @return
     */
    @ApiImplicitParam(
            name = "account",
            value = "账号",
            dataTypeClass = String.class,
            required = true
            ,paramType = "query"
    )
    @ApiOperation("查询账号是否存在")
    @GetMapping({"/supplier/check"})
    public AjaxResult check(@RequestParam(required = true) String account){
        int rs=supplierUserService.checkUser(account);
        String msg=rs==0?"账号不存在":"账号已存在";
        return AjaxResult.success(msg,rs);
    }
}
